package SpringBoot.spring_boot_starter_websocket.D03_群发或指定用户发;

import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration;
import org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.messaging.simp.SimpMessagingTemplate;

import com.titan.core.utils.common.domain.WebSocketServerMessage;
import com.titan.core.utils.data.DateUtils;
import com.titan.core.utils.spring.SpringUtils;
import com.titan.core.utils.thread.ThreadUtils;
import com.titan.core.web.utils.SpringBootUtils;


@Configuration
@Import({
    // Tomcat，可以使用@RestController, 
    DispatcherServletAutoConfiguration.class,
    EmbeddedServletContainerAutoConfiguration.class,    // 注册容器类型
    ServerPropertiesAutoConfiguration.class,            // 用于初始化容器相关的配置属性，如服务地址、端口、contextPath，并根据当前容器类型初始化各个容器的特有属性，如tomcat的maxThreads、uriEncoding等等，其对应的属性类为ServerProperties；
    HttpEncodingAutoConfiguration.class,                // 注册http编码过滤器
    // SpringMvc，可以访问静态资源
    WebMvcAutoConfiguration.class,                    // *** 注册SpringMvc相关处理器，如ResourceResolver、RequestMappingHandlerAdapter、ExceptionHandlerExceptionResolver、ViewResolver、LocaleResolver，等等
    HttpMessageConvertersAutoConfiguration.class,     // *** 注册json或者xml处理器
    // WebSocket
    WebSocketAutoConfiguration.class,                 // *** 注册webSocket相关处理器，根据容器类型注册不同的处理器
    WebSocketMessagingAutoConfiguration.class,
    // XML
    JacksonAutoConfiguration.class,
})
@ComponentScan(value = { "SpringBoot.spring_boot_starter_websocket.D03_群发或指定用户发" })
public class D03_群发或指定用户发 {
	public static void main(String[] args) {
		new SpringApplicationBuilder()
			.bannerMode(Banner.Mode.OFF)
	        .sources(D03_群发或指定用户发.class)
	        .listeners(SpringBootUtils.registerApplicationListener())
	        .web(true)
	        .run(new String[]{
	                "--debug=false"
	              , "--server.port=9999"
	        });
		
//		ThreadUtils.async(1, new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    SimpMessagingTemplate template = SpringUtils.getBean(SimpMessagingTemplate.class);
//                    while(true) {
//                        template.convertAndSend("/WebSocketTopic/d02", new WebSocketServerMessage(DateUtils.$().toString("yyyy-MM-dd HH:mm:ss")));
//                        ThreadUtils.sleep(3*1000);
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }                
//            }
//		});
	}
}
